package com.zx.system.block.controller.system;

import com.alibaba.fastjson2.JSONObject;
import com.zx.core.wrapper.ListWrapper;
import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.log.annotation.OperateLog;
import com.zx.system.block.param.RoleMenuParam;
import com.zx.system.block.param.RoleParam;
import com.zx.system.block.service.ISysRoleService;
import com.zx.system.block.vo.SysRoleVO;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Create;
import com.zx.validation.groups.StatusUpdate;
import com.zx.validation.groups.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * <p>
 * description: 系统角色 <br>
 * create: 2024-10-29 13:50
 * </p>
 *
 * @author zhou  xun
 */
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class SysRoleController {
    private final ISysRoleService iSysRoleService;


    /**
     * 获取角色列表
     *
     * @param roleParam 查询参数
     * @return {@link JSONObject}
     * @author zhou  xun
     * @since 2024-10-29
     */
    @PreAuthorize("hasAuthority('system:role:list')")
    @OperateLog(value = "获取角色列表")
    @PostMapping("/list")
    public ListWrapper<SysRoleVO> list(RoleParam roleParam) {
        return iSysRoleService.selectRoleVOList(roleParam);
    }

    /**
     * 新增角色
     *
     * @param roleParam 角色信息
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-29
     */
    @PreAuthorize("hasAuthority('system:role:add')")
    @OperateLog(value = "新增角色")
    @PostMapping("/addRole")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "角色参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Create.class})
    )
    public ResponseWrapper addRole(@RequestBody RoleParam roleParam) {
        return iSysRoleService.addRole(roleParam);
    }

    /**
     * 编辑角色
     *
     * @param roleParam 角色信息
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-29
     */
    @PreAuthorize("hasAuthority('system:role:edit')")
    @OperateLog(value = "编辑角色")
    @PutMapping("/editRole")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "角色参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Update.class})
    )
    public ResponseWrapper editRole(@RequestBody RoleParam roleParam) {
        return iSysRoleService.editRole(roleParam);
    }

//    /**
//     * 修改角色状态
//     *
//     * @param jsonStr 参数json字符串
//     * @return {@link ResponseWrapper}
//     * @author zhou  xun
//     * @since 2024-10-29
//     */
//    @PreAuthorize("hasAuthority('system:role:edit')")
//    @OperateLog(value = "修改角色状态")
//    @PutMapping("/editRoleStatus")
//    public ResponseWrapper editRoleStatus(@RequestBody String jsonStr) {
//        ObjectWrapper<RoleParam> objectWrapper = JsonStrUtils.checkParseObject(jsonStr, RoleParam.class);
//        if (!objectWrapper.verifySuccess()) {
//            return objectWrapper;
//        }
//        RoleParam roleParam = objectWrapper.getData();
//        return iSysRoleService.editRoleStatus(roleParam.getId(), roleParam.getStatus());
//    }

    /**
     * 修改角色状态
     *
     * @param roleParam 角色参数
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-29
     */
    @PreAuthorize("hasAuthority('system:role:edit')")
    @OperateLog(value = "修改角色状态")
    @PutMapping("/editRoleStatus")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "角色参数", rules = {ValidateRulesEnum.OBJECT}, groups = {StatusUpdate.class})
    )
    public ResponseWrapper editRoleStatus(@RequestBody RoleParam roleParam) {
        return iSysRoleService.editRoleStatus(roleParam.getId(), roleParam.getStatus());
    }

    /**
     * 删除角色
     *
     * @param roleId 角色Id
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-29
     */
    @PreAuthorize("hasAuthority('system:role:remove')")
    @OperateLog(value = "删除角色")
    @DeleteMapping("/deleteRole/{roleId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "角色Id", rules = {ValidateRulesEnum.NOT_NULL}, groups = {Update.class})
    })
    public ResponseWrapper deleteDept(@PathVariable("roleId") String roleId) {
        return iSysRoleService.deleteRole(roleId);
    }

    /**
     * 获取角色菜单列表
     *
     * @param roleId 角色id
     * @return {@link ObjectWrapper}
     * @author zhou  xun
     * @since 2025-04-14
     */
    @PreAuthorize("hasAuthority('system:role:setRoleMenu')")
    @OperateLog(value = "获取角色菜单列表")
    @GetMapping("/getRoleMenuIdList/{roleId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "角色Id", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ObjectWrapper<Set<String>> getRoleMenuIdList(@PathVariable("roleId") String roleId) {
        return iSysRoleService.selectMenuIdsByRoleId(roleId);
    }

    /**
     * 角色设置权限
     *
     * @param roleMenuParam 角色菜单参数
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2025-04-13
     */
    @PreAuthorize("hasAuthority('system:role:setRoleMenu')")
    @OperateLog(value = "角色设置权限")
    @PutMapping("/roleSetMenu")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "角色菜单参数", rules = {ValidateRulesEnum.OBJECT})
    )
    public ResponseWrapper roleSetMenu(@RequestBody RoleMenuParam roleMenuParam) {
        return iSysRoleService.roleSetMenu(roleMenuParam.getRoleId(), roleMenuParam.getMenuIds());
    }
}